package meterMonitor.controller;

import meterMonitor.biz.MeterBiz;
import meterMonitor.biz.UserBiz;
import meterMonitor.consts.ResponseCode;
import meterMonitor.model.Meter;
import meterMonitor.model.User;
import meterMonitor.vo.BaseResponseData;
import meterMonitor.vo.Page;
import meterMonitor.vo.PageResponse;
import meterMonitor.vo.QueryUserRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * Created by admin on 2019/1/3.
 */
@RequestMapping("userManager")
@RestController
public class UserManagerController extends  BaseController {

    public static final Logger logger = LoggerFactory.getLogger(UserManagerController.class);
    @Autowired
    private UserBiz userBiz;

    @RequestMapping("userManagerIndex.do")
    private ModelAndView index(ModelAndView mv){
        return mv;
    }

    @RequestMapping("query.do")
    @ResponseBody
    private PageResponse<User> query(QueryUserRequest request, Page page){
        this.logger.info("进入查询方法...");
        PageResponse<User> response=new PageResponse<>();
        try{
            List<User> list=userBiz.queryUser(request,page);
            response.setRows(list);
            response.setTotal(page.getTotalCount());
        }catch(Exception ex){
            this.logger.error(ex.getMessage(),ex);
        }
        return response;

//        BaseResponse response = new BaseResponse();
//        List<Product> list = productBiz.queryProductList(params, page);
//        Map<String, Object> data = new HashMap<>();
//        data.put("list", list);
//        data.put("total", page.getTotalCount());
//        response.setData(data);
//        return response;
    }

    @RequestMapping("saveMeter.do")
    @ResponseBody
    private BaseResponseData queryMeter(Meter meter){
        BaseResponseData response = new BaseResponseData();
        try {
            //this.meterBiz.saveMeter(meter);
            response.setCode(ResponseCode.SUCCESS);
//            List<Meter> list = meterBiz.queryMeter(null);
//            return list;
        }catch (Exception ex){
            response.setCode(ResponseCode.DEFAULT_EXCEPTION);
            response.setMessage("系统异常！请联系管理员");
        }
        return response;
    }



//    @Bean
//    public MessageProducer inbound() {
//        MqttPahoMessageDrivenChannelAdapter adapter =
//                new MqttPahoMessageDrivenChannelAdapter("tcp://localhost:1883", "testClient",
//                        "topic1", "topic2");
//        adapter.setCompletionTimeout(5000);
//       adapter.setConverter(new DefaultPahoMessageConverter());
//        adapter.setQos(1);
//        adapter.setOutputChannel(mqttInputChannel());
//        return adapter;
//
//    }
    }
